package com.mszlu.news.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.news.admin.mapper.CategoryMapper;
import com.mszlu.news.admin.model.params.CategoryParam;
import com.mszlu.news.admin.pojo.Category;
import com.mszlu.news.admin.service.CategoryService;
import com.mszlu.news.admin.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result selectCategory(CategoryParam categoryParam) {
        Page<Category> page=new Page<>(categoryParam.getPage(), categoryParam.getPageSize());
        IPage<Category> categoryIPage=categoryMapper.selectCategory(page,categoryParam.getId(),categoryParam.getCategoryName(), categoryParam.getDescription());
        return Result.success(categoryIPage);
    }

    @Override
    public Result insertCategory(CategoryParam categoryParam) {
        Category category=new Category();
        category.setCategoryName(categoryParam.getCategoryName());
        category.setAvatar(categoryParam.getAvatar());
        category.setDescription(categoryParam.getDescription());
        categoryMapper.insert(category);
        return Result.success("添加类型成功");
    }

    @Override
    public Result updateCategory(CategoryParam categoryParam) {
        Category category=new Category();
        category.setId(categoryParam.getId());
        category.setCategoryName(categoryParam.getCategoryName());
        category.setAvatar(categoryParam.getAvatar());
        category.setDescription(categoryParam.getDescription());
        categoryMapper.updateById(category);
        return Result.success("更新类型成功");
    }

    @Override
    public Result deleteCategoryById(Long categoryId) {
        categoryMapper.deleteById(categoryId);
        return Result.success("删除类型成功");
    }
}
